package com.jeesite.modules.app.mockexam.special.multiple.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.app.mockexam.special.record.entity.AppMockexamSpecialRecord;
import com.jeesite.modules.app.mockexam.special.record.service.AppMockexamSpecialRecordService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.mockexam.special.judge.entity.TmockexamSpecialJudge;
import com.jeesite.modules.zx.mockexam.special.multiple.entity.TMockeSpecialMultiple;
import com.jeesite.modules.zx.mockexam.special.multiple.service.TMockeSpecialMultipleService;
import com.jeesite.modules.zx.mockexam.special.single.entity.TmockexamSpecialSingle;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;
import com.jeesite.modules.zx.question.bank.service.TquestionBankService;

@Controller
@RequestMapping(value = "/app/mockexam/special/multiple/")
public class AppMockeSpecialMultipleController {
	@Autowired
	private TMockeSpecialMultipleService tMockeSpecialMultipleService;
	@Autowired
	private AppMockexamSpecialRecordService appMockexamSpecialRecordService;
	@Autowired
	private TquestionBankService tquestionBankService;
	/**
	 * 查询答题记录
	 * @param entity
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "subjectRecordList")
	public void subjectRecordList(TMockeSpecialMultiple entity, Model model,HttpServletRequest request,HttpServletResponse response) {
		List<TMockeSpecialMultiple> subjList = tMockeSpecialMultipleService.findList(entity);
		String theoryId=request.getParameter("theoryId");
		if(StringUtils.isBlank(theoryId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "练习标识");
			return;
		}
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		List<String> subidList=new ArrayList<>();
		for(TMockeSpecialMultiple subject:subjList){
			String objName = subject.getObjName();
			subidList.add(objName);
		}
		if(subidList.size()>0){			
			TquestionBank quebank=new TquestionBank();
			quebank.getSqlMap().getWhere().and("id", QueryType.IN, subidList);
			List<TquestionBank> quetbankList = tquestionBankService.findList(quebank);
			for(TquestionBank question:quetbankList){
				String id = question.getId();
				AppMockexamSpecialRecord theoryRecord=new AppMockexamSpecialRecord();
				theoryRecord.setSpecialId(theoryId);
				theoryRecord.setPostId(entity.getParentCode());
				theoryRecord.setUserId(userId);
				theoryRecord.setQuestionId(id);
				theoryRecord.setType("audio");
				List<AppMockexamSpecialRecord> findList = appMockexamSpecialRecordService.findList(theoryRecord);
				if(findList.size()>0){
					AppMockexamSpecialRecord arecord = findList.get(0);
					String answerId = arecord.getAnswerId();
					BigDecimal score = arecord.getScore();
					question.setAnswer(answerId);
					question.setScore(score.doubleValue());
					if(StringUtils.isNotBlank(answerId)){
						String[] split = answerId.split(",");
						for(String key:split){
						    if(key.equals("1")){
						    	question.setAnswer1("1");
						    }
						    if(key.equals("2")){
						    	question.setAnswer2("2");
						    }
						    if(key.equals("3")){
						    	question.setAnswer3("3");
						    }
						    if(key.equals("4")){
						    	question.setAnswer4("4");
						    }
						    if(key.equals("5")){
						    	question.setAnswer5("5");
						    }
						}
					}
				}
			}
			JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(quetbankList));
			AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
			return;
		  }
		JSONArray parseArray = new JSONArray();
		AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
	}
	/**
	 * 保存专项练习记录
	 */
	@PostMapping(value = "save")
	public void save(AppMockexamSpecialRecord entity,HttpServletRequest request,HttpServletResponse response) {
		 String specialId=entity.getSpecialId();		// 专项练习标识
		 if(StringUtils.isBlank(specialId)){
			 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "主题标识");
			 return;
		 }
		 String type=entity.getType();		// 类型
         if(StringUtils.isBlank(type)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "类型");
			 return;
		 }
		 String userId=entity.getUserId();		// 用户主键
         if(StringUtils.isBlank(userId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			 return;
		 }
		 String answerId=entity.getAnswerId();		// 答案字段
         if(StringUtils.isBlank(answerId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "答案信息");
			 return;
		 }
         String[] split = answerId.split(",");
         List<String> list=new ArrayList<String>();
         for(String key:split){
        	 if(StringUtils.isNotBlank(key)){
        		 list.add(key);
        	 }
         }
		 Integer questionType=entity.getQuestionType();		// 问题类型
		 String questionId=entity.getQuestionId();		// 问题id
		 if(StringUtils.isBlank(questionId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "问题标识");
			 return;
		 }
		 AppMockexamSpecialRecord asrmodel=new AppMockexamSpecialRecord();
         asrmodel.setSpecialId(specialId);
         asrmodel.setType(type);
         asrmodel.setUserId(userId);
         asrmodel.setQuestionId(questionId);
         List<AppMockexamSpecialRecord> findList2 = appMockexamSpecialRecordService.findList(asrmodel);
         String id="";
         if(findList2!=null&&findList2.size()>0){
        	 id=findList2.get(0).getId();
         }
		 //查询问题的答案
		 TMockeSpecialMultiple tss=new TMockeSpecialMultiple();
		 tss.setParentCode(questionId);
		 tss.setObjAnswer("1");
		 List<TMockeSpecialMultiple> findList = tMockeSpecialMultipleService.findList(tss);
		 int a=list.size();
		 int b=findList.size();
		 if(a==b){
			 boolean flag=true;
			 for(TMockeSpecialMultiple tssm:findList){
				 String ansid=tssm.getId();
				 if(!list.contains(ansid)){
					 flag=false;
				 }
			 }
			 if(flag){
				 TMockeSpecialMultiple question = tMockeSpecialMultipleService.get(questionId);
				 Double score = question.getScore();
				 entity.setScore(new BigDecimal(score));
			 }else{
				 entity.setScore(new BigDecimal(0.0));
			 }
		 }else{
			 entity.setScore(new BigDecimal(0.0));
		 }
		 if(StringUtils.isNotBlank(id)){
   			entity.setId(id);
   			appMockexamSpecialRecordService.update(entity);
   		 }else{
		    appMockexamSpecialRecordService.save(entity);
   		 }
		 JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
		 AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	@PostMapping(value = "saveRecord")
	public void saveRecord(AppMockexamSpecialRecord entity,HttpServletRequest request,HttpServletResponse response) {
		String specialId=entity.getSpecialId();		// 专项练习标识
		 if(StringUtils.isBlank(specialId)){
			 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "主题标识");
			 return;
		 }
		 String type=entity.getType();		// 类型
         if(StringUtils.isBlank(type)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "类型");
			 return;
		 }
         String userId=entity.getUserId();		// 用户主键
         if(StringUtils.isBlank(userId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			 return;
		 }
		 String answerId=entity.getAnswerId();		// 答案字段
         if(StringUtils.isBlank(answerId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "答案信息");
			 return;
		 }
         String postId=entity.getPostId();
         if(StringUtils.isBlank(postId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "试卷标识");
			 return;
         }
         String questionId=entity.getQuestionId();		// 问题id
		 if(StringUtils.isBlank(questionId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "问题标识");
			 return;
		 }
		 AppMockexamSpecialRecord asrmodel=new AppMockexamSpecialRecord();
         asrmodel.setSpecialId(specialId);
         asrmodel.setType(type);
         asrmodel.setUserId(userId);
         asrmodel.setPostId(postId);
         asrmodel.setQuestionId(questionId);
         List<AppMockexamSpecialRecord> findList = appMockexamSpecialRecordService.findList(asrmodel);
         String id="";
         if(findList!=null&&findList.size()>0){
        	 id=findList.get(0).getId();
         }
         TquestionBank answerObj = tquestionBankService.get(questionId);
//         TmockexamSpecialSingle  answerObj = tmockexamSpecialSingleService.get(answerId);
   		 String objAnswer = answerObj.getObjAnswer();
   		 if(StringUtils.isNotBlank(objAnswer)&&objAnswer.equals(answerId)){
//   			TmockexamSpecialSingle question = tmockexamSpecialSingleService.get(questionId);
			 Double score = answerObj.getScore();
			 entity.setScore(new BigDecimal(score));
   		 }else{
   			 entity.setScore(new BigDecimal(0.0));
   		 }
   		 if(StringUtils.isNotBlank(id)){
   			entity.setId(id);
   			appMockexamSpecialRecordService.update(entity);
   		 }else{
		    appMockexamSpecialRecordService.save(entity);
   		 }
   		 JSONObject obj=JSONObject.parseObject(JsonMapper.toJson(entity));
   		 AppController.renderSuccessObject(response, obj, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="findPage")
	public void findPage(TMockeSpecialMultiple entity, HttpServletRequest request,HttpServletResponse response){
		entity.setParentCode("0");
		entity.setPage(new Page<>(request, response));
		Page<TMockeSpecialMultiple> findPage = tMockeSpecialMultipleService.findPage(entity);
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="findList")
	public void findList(TMockeSpecialMultiple entity, HttpServletRequest request,HttpServletResponse response){
		List<TMockeSpecialMultiple> findList = tMockeSpecialMultipleService.findList(entity);
		JSONArray parseObject = JSONObject.parseArray(JsonMapper.toJson(findList));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="listQuestion")
	public void listQuestion(TMockeSpecialMultiple entity, HttpServletRequest request,HttpServletResponse response){
		List<TMockeSpecialMultiple> findList = tMockeSpecialMultipleService.findList(entity);
		List<String> questionIds=new ArrayList<>();
		for(TMockeSpecialMultiple single:findList){
			String objName = single.getObjName();
			questionIds.add(objName);
		}
		if(questionIds.size()>0){
			TquestionBank qustion=new TquestionBank();
			qustion.getSqlMap().getWhere().and("id", QueryType.IN, questionIds);
			List<TquestionBank> findList2 = tquestionBankService.findList(qustion);
			JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(findList2));
			AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
			return;
		}else{
			AppController.renderSuccessObject(response, "", MessageCode.SUCCESS_ACK);
		}
		
	}
	@RequestMapping(value="optionList")
	public void optionList(TMockeSpecialMultiple entity, HttpServletRequest request,HttpServletResponse response){
		AppMockexamSpecialRecord appMockexamSpecialRecord=new AppMockexamSpecialRecord();
		appMockexamSpecialRecord.setType("multiple");
		String specialId=request.getParameter("specialId");
		if(StringUtils.isBlank(specialId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "查询标识");
			return;
		}
		appMockexamSpecialRecord.setSpecialId(specialId);
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		appMockexamSpecialRecord.setUserId(userId);
		String questionId=request.getParameter("questionId");
		if(StringUtils.isBlank(questionId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "问题标识");
			return;
		}
		appMockexamSpecialRecord.setQuestionId(questionId);
		List<AppMockexamSpecialRecord> recordList = appMockexamSpecialRecordService.findList(appMockexamSpecialRecord);
		List<String> list=new ArrayList<>();
		for(AppMockexamSpecialRecord recor:recordList){
			String answerId = recor.getAnswerId();
			for(String key:answerId.split(",")){
				list.add(key);
			}
		}
		List<TMockeSpecialMultiple> findList = tMockeSpecialMultipleService.findList(entity);
		for(TMockeSpecialMultiple single:findList){
			String id = single.getId();
			if(list.contains(id)){
				single.setChecked(true);
			}
		}
		JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(findList));
		AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="questionList")
	public void questionList(TMockeSpecialMultiple entity, HttpServletRequest request,HttpServletResponse response){
		AppMockexamSpecialRecord appMockexamSpecialRecord=new AppMockexamSpecialRecord();
		appMockexamSpecialRecord.setType("multiple");
		String specialId=request.getParameter("specialId");
		if(StringUtils.isBlank(specialId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "查询标识");
			return;
		}
		appMockexamSpecialRecord.setSpecialId(specialId);
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		appMockexamSpecialRecord.setUserId(userId);
		List<TMockeSpecialMultiple> findList = tMockeSpecialMultipleService.findList(entity);
		for(TMockeSpecialMultiple single:findList){
			String id = single.getId();
			appMockexamSpecialRecord.setQuestionId(id);
			List<AppMockexamSpecialRecord> recordList = appMockexamSpecialRecordService.findList(appMockexamSpecialRecord);
			for(AppMockexamSpecialRecord recor:recordList){
				BigDecimal score = recor.getScore();
				single.setScore(score.doubleValue());
			}
		}
		JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(findList));
		AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="detail")
	public void detail(HttpServletRequest request,HttpServletResponse response){
		
	}
	@RequestMapping(value="compute")
	public void compute(HttpServletRequest request,HttpServletResponse response){
		Enumeration<String> parameterNames = request.getParameterNames();
    	TMockeSpecialMultiple entity =new TMockeSpecialMultiple();
    	int success=0;
    	int error=0;
    	Map<String, List<String>> map=new HashMap<>();
    	while(parameterNames.hasMoreElements()){
    		String key=parameterNames.nextElement();
    		if(!key.equals("userCode")){
	    		String value=request.getParameter(key);
	    		String[] split = value.split(",");
	    		List<String> list=new ArrayList<String>();
	    		for(String obj:split){
	    			list.add(obj);	    			
	    		}
	    		map.put(key, list);
	    		entity.setParentCode(key);
	    		entity.setObjAnswer("1");
	    		List<TMockeSpecialMultiple> findList = tMockeSpecialMultipleService.findList(entity);
	    		if(findList!=null&&findList.size()>0){
	    			boolean add=true;
	    			for(TMockeSpecialMultiple mov:findList){
		    			String id = mov.getId();
		    			if(!list.contains(id)){
		    				add=false;
		    			}
		    		}
	    			if(add){
		    			success+=1;
		    		}else{
		    			error+=1;
		    		}
	    		}	    		
    	    }
    	}
    	JSONObject json=new JSONObject();
    	json.put("success", success);
    	json.put("error", error);
    	AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
}
